#include <stdio.h>
#include <ctype.h>

int main(){
    char ch;
    int hour, minute, total_minutes;
    int t1 = 8 * 60, t2 = 9 * 60 + 43, t3 = 11 * 60 + 19, t4 = 12 * 60 + 47, t5 = 14 * 60, t6 = 15 * 60 + 45, t7 = 19 * 60, t8 = 21 * 60 + 45;

    printf("Enter a 12-hour time: ");
    scanf("%d :%d %c", &hour, &minute, &ch);
    hour = hour == 12 ? 0 : hour;
    if (toupper(ch) == 'A') {
        total_minutes = hour * 60 + minute;
    } else if (toupper(ch) == 'P') {
        total_minutes = hour * 60 + minute + 12 * 60;
    } else {
        printf("Wrong time input!");
        return 0;
    }

    printf("Closest departure time is ");
    if (total_minutes <= (t1 + t2) / 2){
        printf("8:00 a.m. arriving at 10:16 a.m.\n");
    } else if (total_minutes <= (t2 + t3) / 2) {
        printf("9:43 a.m. arriving at 11:52 a.m.\n");
    } else if (total_minutes <= (t3 + t4) / 2) {
        printf("11:19 a.m. arriving at 1:31 p.m.\n");
    } else if (total_minutes <= (t4 + t5) / 2) {
        printf("12:47 p.m. arriving at 3:00 p.m.\n");
    } else if (total_minutes <= (t5 + t6) / 2) {
        printf("2:00 p.m. arriving at 4:08 p.m.\n");
    } else if (total_minutes <= (t6 + t7) / 2) {
        printf("3:45 p.m. arriving at 5:55 p.m.\n");
    } else if (total_minutes <= (t7 + t8) / 2) {
        printf("7:00 p.m. arriving at 9:20 p.m.\n");
    } else {
        printf("9:45 p.m. arriving at 11:58 p.m.\n");
    }

    return 0;
}
